package net.magicidea;

import java.util.Calendar;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBHelper extends SQLiteOpenHelper {

	private static DBHelper instance;
	private static final String TAG = DBHelper.class.getSimpleName();
	private static final int DATABASE_VERSION = 3;
	private static final String DATABASE_NAME = "ESUN_PLUS";
    public static final String BOOK_TABLE_NAME = "BOOK";
    private static final String BOOK_TABLE_CREATE = "CREATE TABLE " + BOOK_TABLE_NAME + "(" +
    		"BOOK_TIME DATETIME PRIMARY KEY," +
    		"AMOUNT INTEGER," +
    		"MSG VARCHAR" +
    		")";

    private DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(BOOK_TABLE_CREATE);
        
        Calendar cal = Calendar.getInstance();
        ContentValues cv = new ContentValues();
        
        cv.put("book_time", cal.getTimeInMillis());
        cv.put("amount", 1200);
        cv.put("msg", "cost 1200");
        db.insert("book", null, cv);
        
        cal.add(Calendar.MONTH, 1);
        cv.put("book_time", cal.getTimeInMillis());
        cv.put("amount", 2400);
        cv.put("msg", "cost 2400");
        db.insert("book", null, cv);
        
        cal.add(Calendar.MONTH, 1);
        cv.put("book_time", cal.getTimeInMillis());
        cv.put("amount", 3600);
        cv.put("msg", "cost 3600");
        db.insert("book", null, cv);
        
        
        Log.i(TAG, "Esun Plus Database Created");
    }

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		
	}
	
	public static DBHelper getInstance(Context ctx)
	{
		if(instance == null)
		{
			synchronized (DBHelper.class) {
				if(instance == null)
				{
					instance = new DBHelper(ctx);
				}
			}
		}
		
		return instance;
	}
}
